/*
 * @Author: wuyang
 * @Description: 
 * @Date: 2023-01-03 16:04:03
 * @LastEditors: wuyang
 * @LastEditTime: 2023-01-03 16:09:30
 * @FilePath: \vue-ts-vite\src\utils\validate.ts
 */
/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path:string) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str:string) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}


/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str:string) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str:string) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str:string) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email:string) {
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email)
}



/**
 * 判断是不是数组
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg:string) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}

/**
 * 校验名称（只能包含字母，中文，数字和下划线）
 * @param {string} name
 * @returns boolean
 */
export function validateName(name:string) {
  const exec = /^[\u4e00-\u9fa5_a-zA-Z0-9]+$/
  if (name) {
    return exec.test(name)
  }
  return true
}
